package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.Hiskeyrecord;
import com.esm.sm800.database.greendao.HiskeyrecordDao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HiskeyrecordCenter {

    private static HiskeyrecordCenter instance = new HiskeyrecordCenter();

    public static HiskeyrecordCenter getInstance() {
        return instance;
    }

     public Hiskeyrecord getHiskeyrecord(String name, int type) {
        Hiskeyrecord hiskeyrecord = null;
        try {
            HiskeyrecordDao HiskeyrecordDao = AppContext.getInstances().getDaoSession().getHiskeyrecordDao();
            ArrayList<Hiskeyrecord> list = new ArrayList<Hiskeyrecord>();
            list = (ArrayList) HiskeyrecordDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.HiskeyrecordDao.Properties.Type.eq(type))
                    .where(com.esm.sm800.database.greendao.HiskeyrecordDao.Properties.Name.eq(name))
                    .build().list();
            if (list.size() > 0) {
                hiskeyrecord = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return hiskeyrecord;
    }

    public Long saveHiskeyrecord(Hiskeyrecord hiskeyrecord) {
        if (hiskeyrecord == null) {
            return 0L;
        }

        Hiskeyrecord a = getHiskeyrecord(hiskeyrecord.getName(), hiskeyrecord.getType());
        if (a != null) {
            hiskeyrecord.setId(a.getId());
        }

        try {
            HiskeyrecordDao HiskeyrecordDao = AppContext.getInstances().getDaoSession().getHiskeyrecordDao();
            long id = HiskeyrecordDao.insertOrReplace(hiskeyrecord);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public Long insertHiskeyrecord(Hiskeyrecord Hiskeyrecord) {
        if (Hiskeyrecord == null) {
            return 0L;
        }

        try {
            HiskeyrecordDao HiskeyrecordDao = AppContext.getInstances().getDaoSession().getHiskeyrecordDao();
            long id = HiskeyrecordDao.insertOrReplace(Hiskeyrecord);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public ArrayList<Hiskeyrecord> getHiskeyrecordlist(int type) {
        ArrayList<Hiskeyrecord> list = new ArrayList<Hiskeyrecord>();
        try {
            HiskeyrecordDao HiskeyrecordDao = AppContext.getInstances().getDaoSession().getHiskeyrecordDao();
            list = (ArrayList) HiskeyrecordDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.HiskeyrecordDao.Properties.Type.eq(type))
                    .orderDesc(com.esm.sm800.database.greendao.HiskeyrecordDao.Properties.Update_time)
                    .limit(10)
                    .build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
